// cn/iocoder/yudao/module/gold/controller/admin/record/GoldMaterialRecordController.java
package cn.iocoder.yudao.module.system.controller.admin.warehouse;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.system.controller.admin.warehouse.vo.*;
import cn.iocoder.yudao.module.system.convert.warehouse.GoldMaterialRecordConvert;
import cn.iocoder.yudao.module.system.dal.dataobject.warehouse.GoldMaterialRecordDO;
import cn.iocoder.yudao.module.system.service.warehouse.GoldMaterialRecordService;
import io.swagger.v3.oas.annotations.Operation;
import com.mzt.logapi.starter.annotation.LogRecord;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.*;

@RestController
@RequestMapping("/gold/material/record")
public class GoldMaterialRecordController {

    @Resource
    private GoldMaterialRecordService service;

    // 原来的统一入库接口（可以保留或删除）
    @PostMapping("/in")
    @PreAuthorize("@ss.hasPermission('gold:material-record:in')")
    @LogRecord(type = "金料出入库", subType = "统一入库", bizNo = "{{#reqVO.warehouseId}}", 
               extra = "{{#reqVO}}", success = "入库成功")
    public CommonResult<Long> in(@Valid @RequestBody GoldInReqVO reqVO) {
        return success(service.in(reqVO, getLoginUserId(), getLoginUserNickname()));
    }

    // 新增：买料入料接口
    @PostMapping("/buy-in")
    @PreAuthorize("@ss.hasPermission('gold:material-record:buy-in')")
    @LogRecord(type = "金料出入库", subType = "买料入料", bizNo = "{{#reqVO.warehouseId}}", 
               extra = "{{#reqVO}}", success = "买料入料成功")
    public CommonResult<Long> buyIn(@Valid @RequestBody GoldInReqVO reqVO) {
        return success(service.in(reqVO, getLoginUserId(), getLoginUserNickname()));
    }

    // 新增：销售回料接口
    @PostMapping("/sales-return")
    @PreAuthorize("@ss.hasPermission('gold:material-record:sales-return')")
    @LogRecord(type = "金料出入库", subType = "销售回料", bizNo = "{{#reqVO.warehouseId}}", 
               extra = "{{#reqVO}}", success = "销售回料成功")
    public CommonResult<Long> salesReturn(@Valid @RequestBody GoldInReqVO reqVO) {
        return success(service.in(reqVO, getLoginUserId(), getLoginUserNickname()));
    }

    @PostMapping("/reduce")
    @PreAuthorize("@ss.hasPermission('gold:material-record:reduce')")
    @LogRecord(type = "金料出入库", subType = "采料出料", bizNo = "{{#reqVO.warehouseId}}",
               extra = "{{#reqVO}}", success = "减少库存成功")
    public CommonResult<Long> reduce(@Valid @RequestBody GoldReduceReqVO reqVO) {
        return success(service.reduce(reqVO, getLoginUserId(), getLoginUserNickname()));
    }

    @GetMapping("/page")
    //@PreAuthorize("@ss.hasPermission('gold:material-record:query')")
    public CommonResult<PageResult<GoldMaterialRecordRespVO>> page(@Valid GoldMaterialRecordPageReqVO reqVO) {
        PageResult<GoldMaterialRecordDO> page = service.getPage(reqVO);
        List<GoldMaterialRecordRespVO> list = GoldMaterialRecordConvert.INSTANCE.convertList(page.getList());
        return success(new PageResult<>(list, page.getTotal()));
    }

    @GetMapping("/stats")
    @Operation(summary = "查询操作统计")
    //@PreAuthorize("@ss.hasPermission('gold:material-record:query')")
    public CommonResult<GoldMaterialRecordStatsRespVO> getRecordStats(@RequestParam(required = false) Long warehouseId) {

        GoldMaterialRecordStatsRespVO stats = service.getRecordStats(warehouseId);


        return success(stats);
    }

    @GetMapping("/global-stats")
    @Operation(summary = "查询全局统计")
    public CommonResult<GoldMaterialGlobalStatsRespVO> getGlobalStats() {
        GoldMaterialGlobalStatsRespVO globalStats = service.getGlobalStats();
        return success(globalStats);
    }
}